package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.ParamModel;
import com.goods.common.model.business.ProductCategoryTreeNode;
import com.goods.common.model.business.Supplier;
import com.goods.common.model.system.User;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 赵勇镔
 * @Time: 2021/6/8 11:50
 * @Description:
 */

//物资来源实体类
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired(required = false)
    private SupplierMapper supplierMapper;


    //物资来源条件查询显示
    @Override
    public PageVO<SupplierVO> findSupplierList(ParamModel paramModel) {

        //开启分页
        PageHelper.startPage(paramModel.getPageNum(), paramModel.getPageSize());

        //从数据库中查询
        Example o = new Example(Supplier.class);
        //具体地点
        String name = paramModel.getName();
        //联系人
        String contact = paramModel.getContact();
        //省市区县
        String address = paramModel.getAddress();
        Example.Criteria criteria = o.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (contact != null && !"".equals(contact)) {
            criteria.andLike("contact", "%" + contact + "%");
        }
        if (address != null && !"".equals(address)) {
            criteria.andLike("address", "%" + address + "%");
        }
        List<Supplier> supplierList = supplierMapper.selectByExample(o);

        //转化为需要的类型
        List<SupplierVO> supplierVOList = new ArrayList<>();
        //遍历转化为封装类
        for (Supplier supplier : supplierList) {
            //使用封装的方法转化
            SupplierVO supplierVO = SupplierConverter.converterSupplierVO(supplier);
            //添加到集合
            supplierVOList.add(supplierVO);
        }

        //转化为分页对象返回
        PageInfo<Supplier> Info = new PageInfo<>(supplierList);
        //前端返回需要的对象
        return new PageVO<>(Info.getTotal(), supplierVOList);

    }

    //物资来源添加
    @Override
    public void add(SupplierVO supplierVO) {
        //设置创建修改时间
        supplierVO.setCreateTime(new Date());
        supplierVO.setModifiedTime(new Date());
        supplierMapper.insert(SupplierConverter.converterSupplier(supplierVO));
    }

    //物资来源修改数据回显
    @Override
    public Supplier edit(Long id) {
        Example o = new Example(Supplier.class);
        o.createCriteria().andEqualTo("id", id);

        return supplierMapper.selectOneByExample(o);
    }

    //物资来源修改数据保存
    @Override
    public void update(Long id, SupplierVO supplierVO) {
        //先转化类型
        Supplier supplier = SupplierConverter.converterSupplier(supplierVO);
        supplier.setModifiedTime(new Date());

        Example o = new Example(Supplier.class);
        o.createCriteria().andEqualTo("id",id);
        //根据id保存修改内容
        supplierMapper.updateByExample(supplier,o);
    }

    //物资来源修改数据删除
    @Override
    public void delete(Long id) {
        Example o = new Example(Supplier.class);
        o.createCriteria().andEqualTo("id",id);
        //根据id保存修改内容
        supplierMapper.deleteByExample(o);
    }

    //不分页查所有
    @Override
    public List<SupplierVO> findAll() {
        List<Supplier> supplierList = supplierMapper.selectAll();
        List<SupplierVO> supplierVOS = new ArrayList<>();
        for (Supplier supplier:supplierList) {
            SupplierVO supplierVO = SupplierConverter.converterSupplierVO(supplier);
            supplierVOS.add(supplierVO);
        }
        return supplierVOS;
    }


}
